
//
//  SerchOraganizaitonViewController.swift
//  VenusMaster
//
//  Created by weimengran on 2018/12/21.
//  Copyright © 2018 zhaowang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxAtomic

class OriganizaionSearchBar: UIView {
    var textField = UITextField.init()
    let cancelBtn = UIButton.init()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(textField)
        textField.frame = CGRect.init(x: 5, y: frame.height - 44, width: frame.width - 10 - 50, height: 30);
        backgroundColor = UIColor(hex: "17bbfe")
        textField.backgroundColor = UIColor.white
        textField.layer.cornerRadius = 15
        textField.clipsToBounds = true
        textField.placeholder = "点击此处搜索机构"
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.leftViewMode = .always
        textField.leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
        addSubview(cancelBtn)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.frame = CGRect.init(x: frame.width - 50, y: frame.height - 44, width: 40, height: 30)
        cancelBtn.titleLabel?.textColor = UIColor.red
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class SerchOraganizaitonViewController: UIViewController{
    var searchBar = OriganizaionSearchBar.init(frame: CGRect.init(x: 0, y: 0, width: LayoutCommon.width, height: LayoutCommon.navHeight))
    var tableView = UITableView.init(frame: CGRect.init(x: 0, y:LayoutCommon.navHeight, width: LayoutCommon.width, height:  LayoutCommon.height - LayoutCommon.navHeight))
    
    var viewModel : SearchOraganizaionViewModel?
    
    fileprivate lazy var dispose: DisposeBag = DisposeBag()

    init(vm : SearchOraganizaionViewModel ) {
        super.init(nibName: nil, bundle: nil)
        viewModel = vm
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(searchBar)
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
     unowned let  weakSelf = self
        searchBar.cancelBtn.rx.tap.subscribe(onNext: {
           weakSelf.navigationController?.popViewController(animated: true)
        }).disposed(by: dispose)
       let searchText = searchBar.textField.rx.text.orEmpty.asObservable()
        searchText.subscribe(onNext : { text in
            weakSelf.viewModel?.searchWithKeyWords(keyWork: text, loadMore: false)
        }).disposed(by: dispose)
        
        self.viewModel?.subject.subscribe(onNext:{(ary) in
            self.tableView.reloadData()
        }).disposed(by: dispose)
        
        self.viewModel?.subject.subscribe(onError:{ (error : Error) in
            let code = (error as NSError).code
            if (code != -999) {
                self.hudLoadMessage(error.localizedDescription)
            }
        }).disposed(by: dispose)
        tableView.register(UINib(nibName: "HomeTableViewCell", bundle: nil), forCellReuseIdentifier: "HomeTableViewCell")

    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchBar.textField.becomeFirstResponder()
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
     super.viewWillDisappear(animated)
        searchBar.endEditing(true)
        
    }
    @objc func popBack(btn : UIButton) -> Void {
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension SerchOraganizaitonViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 96
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.viewModel?.modelObserable.count)!
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
        cell.configModel(model: (viewModel?.modelObserable[indexPath.item])!, index: indexPath)
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = SchoolDetailViewController()
        let model = viewModel!.modelObserable[indexPath.item]
        vc.schoolID = "\(model.id)"
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        searchBar.textField.endEditing(true)
    }
}
